/*
 * Copyright 2016 jeasonlzy(廖子尧)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.cofeed.callback;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.KeyEvent;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ObjectUtils;
import  com.example.cofeed.util.ToastUtils;;
import com.example.cofeed.R;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import java.net.ConnectException;
import java.net.UnknownHostException;

/**
 * ================================================
 * 作    者：jeasonlzy（廖子尧）Github地址：https://github.com/jeasonlzy
 * 版    本：1.0
 * 创建日期：2016/1/14
 * 描    述：对于网络请求是否需要弹出进度对话框
 * 修订历史：
 * ================================================
 */
public abstract class DialogCallback<T> extends JsonCallback<T> {

    private Dialog dialog;

    private void initDialog(Context activity) {
        if (dialog == null) {
            dialog = new Dialog(activity);
            dialog.setCanceledOnTouchOutside(false);
            dialog.setContentView(R.layout.dialog_loading);
//            dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                    if (i == KeyEvent.KEYCODE_BACK) {
                    }
                    return false;
                }
            });
        }

    }

    public DialogCallback(Context activity) {
        super();
        if (ObjectUtils.isNotEmpty(activity)) {
            initDialog(activity);

        }
    }

    public DialogCallback() {
        super();

    }

    @Override
    public void onStart(Request<T, ? extends Request> request) {
        super.onStart(request);
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    @Override
    public void onFinish() {
        super.onFinish();
        //网络请求结束后关闭对话框
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    public void onError(Response<T> response) {
        super.onError(response);

        LogUtils.e("-------------------error" + response.message());
        if (response.getException() instanceof ConnectException || response.getException() instanceof UnknownHostException) {
            ToastUtils.showShort("网络连接不可用，请稍后重试");
        } else if (response != null && !TextUtils.isEmpty(response.getException().getMessage())) {
            ToastUtils.showShort(response.getException().getMessage());
        }
    }
}
